﻿using System;
using System.Configuration;
using DynamicComparer;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;


namespace Shuba.Web.master
{
    public partial class flag_freeze_list : basePage
    {
        string msg = "";
        bool IsView, IsAdd, IsEdit, IsDelete, IsChecker;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultFocus = txtSearch.ClientID;
            this.Form.DefaultButton = btnSearch.UniqueID;

            string acl = GetUserAccess("FLAG_FREEZE");

            IsView = IsUserView(acl);
            IsAdd = IsUserAdd(acl);
            IsEdit = IsUserEdit(acl);
            IsDelete = IsUserDelete(acl);
            IsChecker = IsUserChecker(acl);

            if (!IsView)
            {
                msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {
                InitSort();
                BindData();

                LabelTitle.Text = GetPageTitle("FLAG_FREEZE");

                if (IsAdd)
                {
                    btnAdd.Enabled = true;
                    btnUpload.Enabled = true;
                }
                else
                {
                    btnAdd.Enabled = false;
                    btnUpload.Enabled = false;
                }
            }
        }

        private void BindData()
        {
            Shuba.Business.FlagFreeze bl = new Shuba.Business.FlagFreeze();
            List<Shuba.Model.FlagFreezeInfo> list;

            list = bl.GetList(txtSearch.Text);
            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<Shuba.Model.FlagFreezeInfo> comparer = new DynamicComparer<Shuba.Model.FlagFreezeInfo>(sort);
                list.Sort(comparer);
            }

            gvList.DataSource = list;
            gvList.DataBind();

            if (IsEdit || IsDelete)
            {
                gvList.Columns[gvList.Columns.Count - 1].Visible = true;
            }
            else gvList.Columns[gvList.Columns.Count - 1].Visible = false;

            //FOR APPROVAL GRID
            if (IsChecker)
            {
                BindApprovalData();
                if (ApprovalGrid.Rows.Count > 0) { ApprovalPnl.Visible = true; } else { ApprovalPnl.Visible = false; }
            }
        }

        protected void gvList_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gvList_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Edit":
                    Response.Redirect("flag_freeze_add.aspx?a=ed&id=" + e.CommandArgument);
                    break;
                case "Remove":
                    string[] ParamID = e.CommandArgument.ToString().Split('~');
                    Int16 flag = Convert.ToInt16(ParamID[0]);
                    string field = ParamID[1].ToString().Trim();

                    Shuba.Business.FlagFreeze bl = new Shuba.Business.FlagFreeze();

                    Shuba.Model.FlagFreezeInfo bi = bl.Get(flag, field);
                    bi.UPDATE_BY = HttpContext.Current.User.Identity.Name;
                    bi.UPDATE_DATE = ProcessingDate();
                    bi.ACT_TYPE = "DELETE";
                    try
                    {
                        bl.Save(bi);
                        AddLog("DELETE", "DELETE FLAG");
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message.ToString();
                        ErrBox.Visible = true;
                        ErrMsg.Text = msg;
                    }
                    if (msg == "") RestartPage();
                    break;
            }
        }

        protected void gvList_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btnEdit = (LinkButton)e.Row.FindControl("btnEdit");
                LinkButton btnDelete = (LinkButton)e.Row.FindControl("btnDelete");

                if (IsEdit) btnEdit.Enabled = true; else btnEdit.Enabled = false;

                if (IsDelete && e.Row.Cells[gvList.Columns.Count - 2].Text == "1")
                {
                    btnDelete.Enabled = true;
                }
                else
                {
                    btnDelete.Enabled = false;
                    btnDelete.OnClientClick = "";
                }

                if (e.Row.Cells[gvList.Columns.Count - 2].Text == "" || e.Row.Cells[gvList.Columns.Count - 2].Text == "1")
                {
                    e.Row.Cells[gvList.Columns.Count - 2].Text = "Active";

                    e.Row.ForeColor = System.Drawing.Color.Black;
                    e.Row.Font.Bold = false;
                }
                else
                {
                    e.Row.Cells[gvList.Columns.Count - 2].Text = "Inactive";

                    e.Row.ForeColor = System.Drawing.Color.Red;
                    e.Row.Font.Bold = true;

                    e.Row.Cells[e.Row.Cells.Count - 2].ForeColor = System.Drawing.Color.Black;
                    e.Row.Cells[e.Row.Cells.Count - 2].Font.Bold = false;
                }

                if (e.Row.Cells[2].Text == "1"){  e.Row.Cells[2].Text = "Enable";}
                else if (e.Row.Cells[2].Text == "0") { e.Row.Cells[2].Text = "Disable"; }
            }
        }

        protected void gvList_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            gvList.PageIndex = 0;
            BindData();
        }

        /*======================================
                APPROVAL GRID START HERE
         =======================================
         */
        private void BindApprovalData()
        {
            Shuba.Business.FlagFreeze bl = new Shuba.Business.FlagFreeze();
            List<Shuba.Model.FlagFreezeInfo> list;

            list = bl.GetListApproval(HttpContext.Current.User.Identity.Name);
            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<Shuba.Model.FlagFreezeInfo> comparer = new DynamicComparer<Shuba.Model.FlagFreezeInfo>(sort);
                list.Sort(comparer);
            }
            ApprovalGrid.DataSource = list;
            ApprovalGrid.DataBind();
        }

        protected void ApprovalGrid_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            ApprovalGrid.PageIndex = e.NewPageIndex;
            BindApprovalData();
        }

        protected void ApprovalGrid_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            ApprovalGrid.PageIndex = 0;
            BindApprovalData();
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ApprovalGrid.Rows)
            {
                CheckBox chkItemSelect = (CheckBox)row.FindControl("chkItemSelect");
                string temp = ApprovalGrid.DataKeys[row.RowIndex].Value.ToString();
                Int16 flag = Convert.ToInt16(temp);
                string field = ApprovalGrid.DataKeys[row.RowIndex].Values["FIELD"].ToString();

                if (chkItemSelect.Checked)
                {
                    Approve(flag, field, true);
                }
            }
            msg = this.GetGlobalResourceObject("Messages", "ApproveAlert").ToString();
            BindData();
            Alert(msg);
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ApprovalGrid.Rows)
            {
                CheckBox chkItemSelect = (CheckBox)row.FindControl("chkItemSelect");
                Int16 flag = Convert.ToInt16(ApprovalGrid.DataKeys[row.RowIndex].Value.ToString());
                string field = ApprovalGrid.DataKeys[row.RowIndex].Values["FIELD"].ToString();
                if (chkItemSelect.Checked)
                {
                    Approve(flag, field, false);
                }
            }
            msg = this.GetGlobalResourceObject("Messages", "NoApprove").ToString();
            BindData();
            Alert(msg);
        }

        private void Approve(Int16 flag, string field, bool IsApprove)
        {
            string ActType;

            Shuba.Business.FlagFreeze bl = new Shuba.Business.FlagFreeze();
            Shuba.Model.FlagFreezeInfo bi = new Shuba.Model.FlagFreezeInfo();

            bi = bl.GetApprovalByID(flag, field);
            ActType = bi.ACT_TYPE;
            bi.UPDATE_BY = HttpContext.Current.User.Identity.Name;
            bi.UPDATE_DATE = ProcessingDate();

            if (IsApprove)
            {
                bl.Approve(ActType, flag, field);
                AddLog("APPROVAL", "APPROVE FLAG FREEZE");
            }
            else
            {
                bl.Reject(flag, field);
                AddLog("APPROVAL", "REJECT FLAG FREEZE");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}